package com.redlight.token.security;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chao
 * @Title: BtoAAtoB
 * @date 2019-01-24 21:47
 */
public class BtoAAtoB {
    private static final String base64hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    public BtoAAtoB() {
    }

    public static boolean isMatcher(String inStr, String reg) {
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(inStr);
        return matcher.matches();
    }

    public static String btoa(String inStr) {
        if (inStr != null && !isMatcher(inStr, "([^\\u0000-\\u00ff])")) {
            StringBuilder result = new StringBuilder();
            int i = 0;
            int mod = 0;

            int prev;//改为int
            for(prev = 0; i < inStr.length(); ++i) {
                int ascii = inStr.charAt(i);
                mod = i % 3;
                switch(mod) {
                    case 0:
                        result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(ascii >> 2)));
                        break;
                    case 1:
                        result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((prev & 3) << 4 | ascii >> 4)));
                        break;
                    case 2:
                        result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((prev & 15) << 2 | ascii >> 6)));
                        result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(ascii & 63)));
                }

                prev = ascii;
            }

            if (mod == 0) {
                result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((prev & 3) << 4)));
                result.append("==");
            } else if (mod == 1) {
                result.append(String.valueOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((prev & 15) << 2)));
                result.append("=");
            }

            return result.toString();
        } else {
            return null;
        }
    }

    public static String atob(String inStr) {
        if (inStr == null) {
            return null;
        } else {
            inStr = inStr.replaceAll("\\s|=", "");
            StringBuilder result = new StringBuilder();
            int prev = -1;

            for(int i = 0; i < inStr.length(); ++i) {
                int cur = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(inStr.charAt(i));
                int mod = i % 4;
                switch(mod) {
                    case 0:
                    default:
                        break;
                    case 1:
                        result.append(String.valueOf((char)(prev << 2 | cur >> 4)));
                        break;
                    case 2:
                        result.append(String.valueOf((char)((prev & 15) << 4 | cur >> 2)));
                        break;
                    case 3:
                        result.append(String.valueOf((char)((prev & 3) << 6 | cur)));
                }

                prev = cur;
            }

            return result.toString();
        }
    }
}
